<style type="text/css">
    .disabled { pointer-events: none; }
</style>
    <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=noxDvacPFxnudsybXqwZKdr67SudUupK"></script>
    <script type="text/javascript" src="http://api.map.baidu.com/library/GeoUtils/1.2/src/GeoUtils_min.js"></script>
    <script type="text/javascript" src="http://developer.baidu.com/map/jsdemo/demo/convertor.js"></script>
    <div id="container" style="height:200px;width:100%;"></div>
    <div style="font-size: 14px;padding: 3px 10px;">当前位置：<span id="address">定位中...</span></div>
<script type="text/javascript">
		 function formatDate (date,formatStr) {
            var str = formatStr;
            var Week = ['日', '一', '二', '三', '四', '五', '六'];

            str = str.replace(/yyyy|YYYY/, date.getFullYear());
            str = str.replace(/yy|YY/, (date.getYear() % 100) > 9 ? (date.getYear() % 100).toString() : '0' + (date.getYear() % 100));

            str = str.replace(/MM/, date.getMonth() > 9 ? (date.getMonth() + 1).toString() : '0' + (date.getMonth() + 1));
            str = str.replace(/M/g, date.getMonth());

            str = str.replace(/w|W/g, Week[date.getDay()]);

            str = str.replace(/dd|DD/, date.getDate() > 9 ? date.getDate().toString() : '0' + date.getDate());
            str = str.replace(/d|D/g, date.getDate());

            str = str.replace(/hh|HH/, date.getHours() > 9 ? date.getHours().toString() : '0' + date.getHours());
            str = str.replace(/h|H/g, date.getHours());
            str = str.replace(/mm/, date.getMinutes() > 9 ? date.getMinutes().toString() : '0' + date.getMinutes());
            str = str.replace(/m/g, date.getMinutes());

            str = str.replace(/ss|SS/, date.getSeconds() > 9 ? date.getSeconds().toString() : '0' + date.getSeconds());
            str = str.replace(/s|S/g, date.getSeconds());

            return str;
        }


    var currlng,currlat,kqlng,kqlat,kqfw,kqdd,curraddress;
        var map = new BMap.Map("container");    // 创建Map实例
        var point = new BMap.Point(113.404942,22.520568);
        map.centerAndZoom(point, 15);  // 初始化地图,设置中心点坐标和地图级别
        //map.setCurrentCity("中山");          // 设置地图显示的城市
        map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放
        getCurrentPosition();//获取当前位置并显示在地图上
       // mobile_getCurrentPosition();
       //html5_getCurrentPosition();
        
        //移动建模定位
        function mobile_getCurrentPosition(){
            Mobile_NS.getCurrentPosition(function(result){
                var status = result["status"];
                if(status == "1"){
                    var lng = result["lng"];      /* 精度  -- 31.173211 */
                    var lat = result["lat"];        /* 纬度  -- 121.475134 */
                    var addr = result["addr"];  /* 中文地址  -- 湖北省武汉市江汉区 长青路288号 */
                    curraddress=addr;
                    var baiduPoint = new BMap.Point(lng, lat);
                    initMap(baiduPoint);
                    $("#address").text(addr);
                    
                    setCircleLocation(lng,lat);
                }else{
                    var errMsg = result["errMsg"];
                    Mobile_NS.Alert("errr:" + errMsg);
                }
            });
        }
        
        function  html5_getCurrentPosition(){
            if (navigator.geolocation){
                navigator.geolocation.getCurrentPosition(function(position){
                    //得到html5定位结果
                    var x = position.coords.longitude;
                    var y = position.coords.latitude;
                    var point = new BMap.Point(x,y);
                    BMap.Convertor.translate(point,0,function(baidupoint){
                        
                        initMap(baidupoint);
                    })
                });
            }else{
                Mobile_NS.Alert("定位失败");
                }
        
        }
        //百度地图浏览器定位
        function getCurrentPosition(){
            var geolocation = new BMap.Geolocation();
            geolocation.getCurrentPosition(function(r){
                if(this.getStatus() == BMAP_STATUS_SUCCESS){
                 
                    var lng =point.lng;      /* 精度  -- 31.173211 */
                    var lat =point.lat;        /* 纬度  -- 121.475134 */
                  
                    var geocoder=new BMap.Geocoder();
                    geocoder.getLocation(r.point,function(result){
                        if(result){
                            console.info(result);
                            curraddress=result.address;
                            $("#address").text(curraddress);
                        }
                       
                    });                
                    initMap(r.point);               
                    setCircleLocation(lng,lat);
                }
                else {
                    console.info('定位失败'+this.getStatus());
                }        
            });
        }
        function initMap(point){
            map.clearOverlays();
            map.centerAndZoom(point, 17);
            var centerMarker = new BMap.Marker(point);
            map.addOverlay(centerMarker);
            map.panTo(centerMarker);
            var scaleControl = new BMap.ScaleControl({anchor: BMAP_ANCHOR_BOTTOM_LEFT});
            map.addControl(scaleControl);
            map.enableScrollWheelZoom();
        }       
        
        //设置考勤点区域范围,并让地图缩放级别能显示当前位置和考勤区域在可视范围
        function setCircleLocation(lng, lat){
            currlng=lng;
            currlat=lat;
            var scurrlng=currlng-0.01;
            var ecurrlng=currlng+0.01;
            var scurrlat=currlat-0.01;
            var ecurrlat=currlat+0.01;
            Mobile_NS.SQL("select jd,wd,kqfw,szdd,jdn from uf_kqdd where jd>{scurrlng} and jd<{ecurrlng} and wd>{scurrlat} and wd<{ecurrlat} order by (Square(jd-{currlng})+Square(wd-{currlat})) desc ;scurrlng="
                    +scurrlng+";ecurrlng="+ecurrlng+";scurrlat="+scurrlat+";ecurrlat="+ecurrlat+";currlng="+currlng+";currlat="+currlat, function(result){
                        console.info(result);
                        if (result.length>0) {
                            for (var i = 0; i < result.length; i++) {
                                var kq_lng=result[i].jd;
                                var kq_lat=result[i].wd;
                                var kq_radius=result[i].kqfw;
                                kqdd=result[i].szdd;
                                var point=new BMap.Point(kq_lng, kq_lat);
                                var circle = new BMap.Circle(point,kq_radius,{strokeColor:"#FF33FF", strokeWeight:3, strokeOpacity:0.2,fillColor:"#1791fc",fillOpacity:0.05}); //创建圆
                                 //画到地图上面
                                map.addOverlay(circle);
                                kqlng=kq_lng;
                                kqlat=kq_lat;
                                kqfw=kq_radius;
                        }
                    }
                    }); 
        }
        
      //提交按钮
        function dk(){
          var centerpoint=new BMap.Point(kqlng, kqlat);
          var currpoint=new BMap.Point(currlng, currlat);
          var circle = new BMap.Circle(centerpoint,kqfw,{strokeColor:"#FF33FF"}); //创建圆
             if(BMapLib.GeoUtils.isPointInCircle(currpoint,circle)){
                 kqinsertDB();
                 disableBtn();
             }else{
                 Mobile_NS.Alert("不在考勤范围", 2);
             }
        }
      //插入考勤数据
     function kqinsertDB(){
         var now = new Date();
         var curruserid=Mobile_NS.getCurrUser();//获取当前用户
         var kqdjwd=currlng+","+currlat;
         Mobile_NS.SQL("insert into uf_kqxx (kqry,kqrq,kqsj,kqdd,kqdjwd,kqdkjd,kqdkwd,kqrdkdd,formmodeid,modedatacreater,modedatacreatertype,modedatacreatedate,modedatacreatetime) values({curruserid},'{kqrq}','{kqsj}','{kqdd}','{kqdjwd}','{kqdkjd}','{kqdkwd}','{curraddress}',2,{curruserid},0,'{kqrq}','{modedatacreatetime}');"+
         "curruserid="+curruserid+";kqrq="+getCurrentDate()+";kqsj="+getCurrentTime()
         +";kqdd="+kqdd+";kqdjwd="+kqdjwd+";kqdkjd="+currlng+";kqdkwd="+currlat+";curraddress="+curraddress+";modedatacreatetime="+getCurrentDateformat('hh:mm:ss'), "", function(res4){
             if(res4 == true){
                    Mobile_NS.Alert("您已成功打卡");
                    updateRecordView();
                }else{
                    Mobile_NS.Alert("打卡失败");
                }
                //Mobile_NS.hideLoader();
            });
     }
      //更新记录显示div
     function updateRecordView(){
         var curruserid=Mobile_NS.getCurrUser();//获取当前用户
         var date=getCurrentDate();
         Mobile_NS.SQL("select concat(kqrq,' ',kqsj) as kqdt ,kqrdkdd from uf_kqxx where kqry={kqry} and kqrq='{kqrq}' order by id desc ;kqry="+curruserid+";kqrq="+date, function(result){
                         //alert(JSON.stringify(result) );
                        var content="";
                        if (result.length>0) {
                            for (var i = 0; i < result.length; i++) {
                                var kqdatetime=result[i].kqdt;
                                var kqrdkdd=result[i].kqrdkdd;
                                content+=kqdatetime+"<br>"	+kqrdkdd+"<br><hr>";
                               banci(kqdatetime);
                            }
                        }
                         $("#record").html("");
                         $("#record").append(content);
                    }); 
     } ;
      
        //获取系统时间
        function getCurrentTime(){
            //格式化时间
            function p(s) {
                return s < 10 ? '0' + s: s;
            }
            var myDate = new Date();
            var h=myDate.getHours();   
            var m=myDate.getMinutes();
            var s=myDate.getSeconds();  
            return p(h)+':'+p(m);
        }

        //获取系统时间
        function getCurrentDate(){
            //格式化时间
            function p(s) {
                return s < 10 ? '0' + s: s;
            }
            var myDate = new Date();
            var year=myDate.getFullYear();    //获取完整的年份(4位,1970-????)
            var month=myDate.getMonth()+1;       //获取当前月份(0-11,0代表1月)
            var day=myDate.getDate();        //获取当前日(1-31)
            return year+'-'+p(month)+'-'+p(day);
        }

        function getCurrentDateformat(patten){
            var now=new Date();
            return formatDate(now,patten);
        }
        function disableBtn(){
            $('#C827776513200001D79FB170B5501D8A').removeAttr("style");
            //$('.mecButton','#NMEC_C827774EF6800001B454BE1D17A91C50').removeAttr("script");
            // $('.mecButton','#NMEC_C827774EF6800001B454BE1D17A91C50').unbind("click");
            $('.mecButton','#NMEC_C827774EF6800001B454BE1D17A91C50').addClass("disabled");
            var css = { "color":"#ffffff","background-color":"#949ca6","border":"#949ca6"};
            $("#C827776513200001D79FB170B5501D8A").css(css);
            $('#C827776513200001D79FB170B5501D8A').children('span').text("已打卡");
        }  

       
   // 班次 type 1,2,3,4   打卡次序,dkdt 打卡时间  "yyyy-MM-dd hh:mm"
   function banci(dkdt){
            var jsontime={
                type1:{time:"08:30",start:"08:00",end:"09:00"},
                type2:{time:"12:00",start:"11:30",end:"12:30"},
                type3:{time:"14:00",start:"13:30",end:"14:30"},
                type4:{time:"18:00",start:"17:30",end:"18:30"}
            };
    
            var type=0;
            var currdatetime=getCurrentDate()+" "+getCurrentTime();
            if (dateTimeStrCompare(currdatetime,getCurrentDate()+" "+jsontime.type1.start)==1&&dateTimeStrCompare(currdatetime,getCurrentDate()+" "+jsontime.type1.end)==-1) {
                type=1;
            }
            else if (dateTimeStrCompare(currdatetime,getCurrentDate()+" "+jsontime.type2.start)==1&&dateTimeStrCompare(currdatetime,getCurrentDate()+" "+jsontime.type2.end)==-1) {
                type=2;
            }
            else if (dateTimeStrCompare(currdatetime,getCurrentDate()+" "+jsontime.type3.start)==1&&dateTimeStrCompare(currdatetime,getCurrentDate()+" "+jsontime.type3.end)==-1) {
                type=3;
            }
            else if(dateTimeStrCompare(currdatetime,getCurrentDate()+" "+jsontime.type4.start)==1&&dateTimeStrCompare(currdatetime,getCurrentDate()+" "+jsontime.type4.end)==-1) {
                type=4;
            }
            
            switch(type)
            {
            case 1:
                var n=dateTimeStrCompare(dkdt,getCurrentDate()+" "+jsontime.type1.start);
                var m=dateTimeStrCompare(dkdt,getCurrentDate()+" "+jsontime.type1.end);
                if ((n==1)&&(m==-1)) {
                    disableBtn();
                }
              break;
            case 2:
                var n=dateTimeStrCompare(dkdt,getCurrentDate()+" "+jsontime.type2.start);
                var m=dateTimeStrCompare(dkdt,getCurrentDate()+" "+jsontime.type2.end);
                if ((n==1)&&(m==-1)) {
                    disableBtn();
                }
              break;
            case 3:
                var n=dateTimeStrCompare(dkdt,getCurrentDate()+" "+jsontime.type3.start);
                var m=dateTimeStrCompare(dkdt,getCurrentDate()+" "+jsontime.type3.end);
                if ((n==1)&&(m==-1)) {
                    disableBtn();
                }
                  break;
            case 4:
                var n=dateTimeStrCompare(dkdt,getCurrentDate()+" "+jsontime.type4.start);
                var m=dateTimeStrCompare(dkdt,getCurrentDate()+" "+jsontime.type4.end);
			
                if ((n==1)&&(m==-1)) {
		
                    disableBtn();
                }
                  break;      
              
            }
        }
        function dateTimeStrCompare(dtstr1,dtstr2){ //"yyyy-MM-dd hh:mm"
            dtstr1= dtstr1.replace("-","/").replace("-","/");//替换字符，变成标准格式  
            var d1 = new Date(Date.parse(dtstr1)); 
            dtstr2= dtstr2.replace("-","/").replace("-","/");//替换字符，变成标准格式  
            var d2 = new Date(Date.parse(dtstr2)); 
            var n=0;
            if(d1>d2){  
                  n=1;
            }else if(d1<d2){
                n=-1;
            } else{
                n=0;
            } 
            return n;
        }



</script>
    
    